<template>
<div class="backstagemain">
    <h2 class="backstageheader">兼职管理</h2>
    <div class="backstagesearch">
        <div class="backstagestatus">
            <label>状态:</label>
            <select v-model="stateSelected">
                <option value="1">已通过</option>
                <option value="2">未通过</option>
                <option value="0">未审核</option>
            </select>
        </div>
        <div class="backstagestatus backstageworktype">
            <label>工作分类:</label>
            <select v-model="typeSelected">
                <option value="">未选择</option>
                <option value="校园外卖">校园外卖</option>
                <option value="跑腿">跑腿</option>
            </select>
        </div>
        <div class="schoolsearch">
            <label>公司名称:</label>
            <input v-model="name" type="text" autocomplete="off" placeholder="请输入公司名称">
        </div>
        <div class="backstagebuttons">
            <button class="backstagesearchbtn" @click="conditionSearch()">搜索</button>
            <button class="backstagenewstart" @click="searchWorks(1)">重置</button>
        </div>
    </div>
    <div class="backstagecontrol"  v-show="message == 1">
        <button type="button" class="backstagedeleteall" @click="deletechoose()">删除所选</button>
    </div>
    <div class="backstagetablediv"  v-show="message == 1">
        <table class="backstagetable">
          <thead>
            <tr>
              <th>选择</th>
              <th>状态</th>
              <th>分类</th>
              <th>公司</th>
              <th>工作地点</th>
              <th>工作名称</th>
              <th>工资</th>
              <th>工作条件</th>
              <th>工作详情</th>
              <th>招聘名额</th>
              <th>操作</th>
            </tr>
          </thead>
            <tbody>
                <WorkadminItem       
                  v-for="(workObj,id) in works" 
                  :key="id"
                  :work="workObj"
                  :searchWorks="searchWorks"
                  :checkThis="checkThis"
                  />
            </tbody>
        </table>
    </div>
    <div class="backstagetablediv" v-show="message == 0">
        <div class="sorry">
            很抱歉，没有找到符合条件的数据
        </div>
    </div>
    <DividePage :maxsize="5"  v-show="message == 1" :changePage="changePage" :itemLength="workLength" :pagesizes="pagesizes"/>
</div>
</template>

<script>
import axios from 'axios'
import DividePage from '../DividePage.vue'
import WorkadminItem from './WorkadminItem.vue'
import {mapState} from 'vuex'
export default {
    name: 'Workadmin',
    components:{WorkadminItem,DividePage},
    // props:['works'],
    data(){
        return{
            works:[],
            workLength:0,
            pagesizes:[5,10],
            ids:[],
            stateSelected:'1',
            typeSelected:'',
            name:'',
            message:1,
        }
    },
    computed: {
        ...mapState({
            dialog: state => state.dialog
        })
    },
    watch:{
        dialog(){
            this.searchWorks(1)
            this.$store.state.select = 1
        }
    },
    methods:{
        changePage(value){
            this.searchWorks(value)
        },
        searchWorks(value){
            // 请求前更新list数据
            var a = this.$store.state.select
            var b = this.pagesizes[a]
            axios.get(`http://123.57.251.41:8080/admin-job/allJobs?pageNo=${value}&pageSize=${b}`,{headers:{'Authentication-Token': JSON.parse(sessionStorage.getItem("token"))}}).then(
                response =>{
                    console.log('请求成功了')
                    console.log(response.data.data.list)
                    this.works = response.data.data.list
                    this.workLength = response.data.data.total
                },
                error =>{
                    alert("请求失败了",console.log(error))
                }
            )
        },
        conditionSearch(){
            if(this.typeSelected == ''){
                if(this.name == ''){
                    axios.get(`http://123.57.251.41:8080/admin-job/mcQueryJob?pageNo=1&pageSize=5&state=${this.stateSelected}`,{headers:{'Authentication-Token': JSON.parse(sessionStorage.getItem("token"))}}).then(
                        response =>{
                            console.log('请求成功了')
                            console.log(response.data.data.list)
                            this.works = response.data.data.list
                            if(response.data.data.list.length == 0){
                                this.message = 0
                            }else{
                                this.message = 1
                            }
                        },
                        error =>{
                            alert("请求失败了",console.log(error))
                        }
                    )
                }else{
                    axios.get(`http://123.57.251.41:8080/admin-job/mcQueryJob?&company=${this.name}&pageNo=1&pageSize=5&state=${this.stateSelected}`,{headers:{'Authentication-Token': JSON.parse(sessionStorage.getItem("token"))}}).then(
                        response =>{
                            console.log('请求成功了')
                            console.log(response.data.data.list)
                            this.works = response.data.data.list
                            if(response.data.data.list.length == 0){
                                this.message = 0
                            }else{
                                this.message = 1
                            }
                        },
                        error =>{
                            alert("请求失败了",console.log(error))
                        }
                    )
                }
            }else if(this.name == ''){
                axios.get(`http://123.57.251.41:8080/admin-job/mcQueryJob?classify=${this.typeSelected}&pageNo=1&pageSize=5&state=${this.stateSelected}`,{headers:{'Authentication-Token': JSON.parse(sessionStorage.getItem("token"))}}).then(
                    response =>{
                        console.log('请求成功了')
                        console.log(response.data.data.list)
                        this.works = response.data.data.list
                        if(response.data.data.list.length == 0){
                            this.message = 0
                        }else{
                            this.message = 1
                        }
                    },
                    error =>{
                        alert("请求失败了",console.log(error))
                    }
                )
            }else{
                axios.get(`http://123.57.251.41:8080/admin-job/mcQueryJob?classify=${this.typeSelected}&company=${this.name}&pageNo=1&pageSize=5&state=${this.stateSelected}`,{headers:{'Authentication-Token': JSON.parse(sessionStorage.getItem("token"))}}).then(
                    response =>{
                        console.log('请求成功了')
                        console.log(response.data.data.list)
                        this.works = response.data.data.list
                        if(response.data.data.list.length == 0){
                            this.message = 0
                        }else{
                            this.message = 1
                        }
                    },
                    error =>{
                        alert("请求失败了",console.log(error))
                    }
                )
            }
        },
        checkThis(a){
            if(this.ids.includes(a)){
                this.ids = this.ids.filter((id)=>{
                    if(id != a){
                        return id
                    }
                })
            }else{
                this.ids.unshift(a)
            }
        },
        deletechoose(){
            if(this.ids.length != 0){
                if(confirm('您确定要删除吗？')){
                    // ids.forEach(id => {
                    //     idarr += `&ids=${id}`
                    // });
                    axios.post(`http://123.57.251.41:8080/admin-job/batchDelJob?ids=${this.ids}`,{headers:{'Authentication-Token': JSON.parse(sessionStorage.getItem("token"))}}).then(
                        response =>{
                            console.log(response)
                            this.searchWorks()
                        },
                        error =>{
                            console.log(id)
                            alert("删除失败了",console.log(error))
                        }
                    )
                }
            }
        }
    },
    mounted(){
        console.log(10086)
        this.searchWorks(1)
    }
}
</script>

<style lang="less" scoped>
</style>